package com.atguigu.lease.common.result;

import lombok.Data;

/**
 * 全局统一返回结果类
 */
@Data
public class Result<T> {

    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    public Result() {
    }

    private static <T> Result<T> build(T data) {
        Result<T> result = new Result<>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }


    public static <T> Result<T> ok(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> ok() {
        return Result.ok(null);
    }

    public static <T> Result<T> fail() {
        return build(null, ResultCodeEnum.FAIL);
    }
    /*
        统一响应类（Result）中的失败响应构建方法:
        创建一个结果对象，数据部分为 null
        设置状态码为失败（通常是 500 或自定义错误码）
        设置错误提示信息
        返回封装好的结果对象
    */
    public static Result<String> fail(String message){
        Result<String> result=build(null);
        result.setCode(ResultCodeEnum.FAIL.getCode());
        result.setMessage(message);//错误信息
        return result;
    }

    public static Result<String> fail(int code,String message){
        Result<String> result=build(null);
        result.setCode(code);//201
        result.setMessage(message);//错误信息
        return result;
    }
}
